package com.chuangsi.project.admin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.chuangsi.project.admin.dto.app.order.FileDto;
import com.chuangsi.project.admin.service.FileService;
import com.chuangsi.project.system.domain.File;
import com.chuangsi.project.system.mapper.FileMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文件表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {

    @Override
    public List<FileDto> getFileList(String sourceId) {
        return  list(Wrappers.lambdaQuery(File.class)
                .eq(File::getSourceId, sourceId))
                .stream().map(FileDto::new).collect(Collectors.toList());
    }

    @Override
    public void removeFile(String sourceId) {
        remove(Wrappers.lambdaUpdate(File.class).eq(File::getSourceId, sourceId));
    }

    @Override
    public List<File> saveFileList(String sourceId,List<FileDto> fileDtoList) {
        List<File> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(fileDtoList)) {
            for (FileDto fileDto : fileDtoList) {
                File file = new File();
                file.setUri(fileDto.getUri());
                file.setFileName(fileDto.getFileName());
                file.setSourceId(sourceId);
                file.setFileType(fileDto.getFileType());
                list.add(file);
            }
            if(!list.isEmpty()){
                saveBatch(list);
            }
        }
        return list;
    }
}
